package com.recipes.meg;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class Choose extends Activity {
	// Declare variables to hold sqlite database and database helper variables
	SQLiteDatabase db;
	DatabaseHelper db_helper;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose);
        
        // Instantiate new db_helper
        db_helper = new DatabaseHelper(this);
        
        // Get the array of recipe ids from the calling activity
        ArrayList<Integer> ids = this.getIntent().getIntegerArrayListExtra("ids");
        
        // Instantiate LinearLayout inside scrollview declared in xml layout file
        LinearLayout l1 = (LinearLayout) findViewById(R.id.chooseLayout);
        
        // Iterate through ids array
        for(int i = 0; i < ids.size(); i++){
        	// Get the current id and save it
        	final int id = ids.get(i);
        	
        	// Open database for reading
        	db = db_helper.getWritableDatabase();
        	
        	// Get the name of the recipe with the current id from the database
        	String temp_name = db_helper.getName(db, id);
        	
        	// Close the database
        	db.close();
        	
        	// Make sure name returned isnt blank
        	if(!temp_name.equalsIgnoreCase("")){
        		// If not then create a button
        		Button temp_button = new Button(this);
        		
        		// Set the text of this button to be the name of this recipe
        		temp_button.setText(temp_name);
        		
        		// Define the layout parameters to be used for this button
        		LinearLayout.LayoutParams temp_params = new LinearLayout.LayoutParams(
        				LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        		
        		// Set all of the margins of this button to be 5
        		temp_params.setMargins(5, 5, 5, 5);
        		
        		// Set the layout parameters of this button
        		temp_button.setLayoutParams(temp_params);
        		
        		// Add this button to the layout
        		l1.addView(temp_button);
        		
        		// Set onClickListener for this button
        		temp_button.setOnClickListener(new View.OnClickListener() {
					// Override onClick button
					@Override
					public void onClick(View v) {
						// Initialize intent to start view recipe activity
						Intent start_view = new Intent(getApplicationContext(), ViewRecipe.class);
					
						// Add the id of this recipe to the start_view intent as an extra
						start_view.putExtra("id", id);
						
						// Use start_view intent to start the view recipe activity
						startActivity(start_view);
						
						// Upon finishing previous activity finish this activity
						finish();
					}
				});
        	}
        }
    }
}
